package com.nwpu.coursesupport.interceptor;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.web.filter.authc.FormAuthenticationFilter;
import org.apache.shiro.web.util.WebUtils;

import com.nwpu.coursesupport.enumeration.StatusType;

public class CustomFormAuthenticationFilter extends FormAuthenticationFilter {

	 @Override
	    protected boolean onAccessDenied(ServletRequest request, ServletResponse response) throws Exception {
	        if (isLoginRequest(request, response)) {
	            if (isLoginSubmission(request, response)) {
	                return executeLogin(request, response);
	            } else {
	                //allow them to see the login page ;)
	                return true;
	            }
	        } else {
	            HttpServletRequest httpRequest = WebUtils.toHttp(request);
	            
	            if (com.nwpu.coursesupport.tool.WebUtils.isAjax(httpRequest)) {
	                
	                HttpServletResponse httpServletResponse = WebUtils.toHttp(response);  
	                com.nwpu.coursesupport.tool.WebUtils.sendJson(httpServletResponse,StatusType.NOLOGIN,"你尚未登录，请登录");
	                return false;
	    
	            } else {  
	                saveRequestAndRedirectToLogin(request, response); 
	            }  

	            return false;
	        }
	    }
}
